package com.nmf.pc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nmf.dao.gen.BizCmsArticleDataMapper;
import com.nmf.dao.gen.BizCmsArticleMapper;
import com.nmf.dao.gen.BizCmsCategoryMapper;
import com.nmf.dao.gen.bean.BizCmsArticle;
import com.nmf.dao.gen.bean.BizCmsArticleData;
import com.nmf.dao.gen.bean.BizCmsArticleExample;
import com.nmf.dao.gen.bean.BizCmsCategory;
import com.nmf.dao.gen.bean.BizCmsCategoryExample;
import com.nmf.pc.vo.ReturnResult;
import com.nmf.pc.vo.req.PubCmsReq;
import com.nmf.pc.vo.rsp.CmsCategoryTileRsp;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IntegerUtils;
import com.nmf.site.modules.cms.entity.Article;
import com.nmf.site.modules.cms.entity.Category;
import com.nmf.site.modules.cms.service.ArticleService;
import com.nmf.site.modules.cms.service.CategoryService;

@Controller
@RequestMapping(value = "/pc")
public class PcCmsController {
	@Autowired
	CategoryService categoryService;
	@Autowired
	ArticleService articleService;
	@Autowired
	BizCmsCategoryMapper bizCmsCategoryMapper;
	/**
	 * 获取分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "p/cms/category" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult category(HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		List<CmsCategoryTileRsp> blist = getAmap("CA0003994256973");
		returnResult.setData(blist);
		return returnResult;
	}
	
	private List<CmsCategoryTileRsp>  getAmap(String categoryId){
		List<CmsCategoryTileRsp> blist = new ArrayList<>();
		List<Category> categoryList = categoryService.findByParentId(categoryId, null);
		for(Category category : categoryList) {
			List<Article>  alist = articleService.queryArticleList(category.getId());
			if(alist.size()>8) {
				alist = alist.subList(0, 8);
			}
			
			BizCmsCategory bizCmsCategory = new BizCmsCategory();
			BeanUtils.copyProperties(category, bizCmsCategory);
			
			List<BizCmsArticle> clist = new ArrayList<BizCmsArticle>();
			for(Article article : alist) {
				BizCmsArticle bizCmsArticle = new BizCmsArticle();
				BeanUtils.copyProperties(article, bizCmsArticle);
				clist.add(bizCmsArticle);
			}
			CmsCategoryTileRsp cmsCategoryTileRsp = new CmsCategoryTileRsp();
			cmsCategoryTileRsp.setBizCmsCategory(bizCmsCategory);
			cmsCategoryTileRsp.setBlist(clist);
			blist.add(cmsCategoryTileRsp);
		}
		return blist;
	}
	@RequestMapping(value = { "p/cms/pubcategory" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult pubcategory(HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCmsCategoryExample bizCmsCategoryExample = new BizCmsCategoryExample();
		bizCmsCategoryExample.createCriteria().andParentIdEqualTo("CA0004002756106");
		List<BizCmsCategory> blist = bizCmsCategoryMapper.selectByExample(bizCmsCategoryExample);
		returnResult.setData(blist);
		return returnResult;
	}
	
	@RequestMapping(value = { "p/cms/articles" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult pubarticle(@Valid PubCmsReq pubCmsReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCmsArticleExample bizCmsArticleExample = new BizCmsArticleExample();
		bizCmsArticleExample.setOrderByClause(" hits desc " );
		bizCmsArticleExample.createCriteria().andCategoryIdEqualTo(pubCmsReq.getCategoryId());
		bizCmsArticleExample.setPage(new Page(pubCmsReq.getPageNo(),pubCmsReq.getLimit()));
		List<BizCmsArticle> blist = bizCmsArticleMapper.selectByExample(bizCmsArticleExample);
		returnResult.setData(blist, bizCmsArticleMapper.countByExample(bizCmsArticleExample));
		return returnResult;
	}
	
	@RequestMapping(value = { "p/cms/polpor" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult polpor(HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		
		BizCmsArticleExample bizCmsArticleExample = new BizCmsArticleExample();
		bizCmsArticleExample.setOrderByClause(" hits desc " );
		bizCmsArticleExample.setPage(new Page(0,10));
		List<BizCmsArticle> blist = bizCmsArticleMapper.selectByExample(bizCmsArticleExample);
		
		returnResult.setData(blist);
		
		return returnResult;
	}
	
	@Autowired
	BizCmsArticleDataMapper bizCmsArticleDataMapper;
	@Autowired
	BizCmsArticleMapper bizCmsArticleMapper;
	@RequestMapping(value = { "p/cms/content" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult cmsContent(@RequestParam(required=true) String articleId,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCmsArticleData bizCmsArticleData = bizCmsArticleDataMapper.selectByPrimaryKey(articleId);
		
		BizCmsArticle bizCmsArticle = bizCmsArticleMapper.selectByPrimaryKey(articleId);
		BizCmsArticle bizCmsArticleRecord = new BizCmsArticle();
		bizCmsArticleRecord.setId(articleId);
		bizCmsArticleRecord.setHits(IntegerUtils.add(bizCmsArticle.getHits(), 1));
		bizCmsArticleMapper.updateByPrimaryKeySelective(bizCmsArticleRecord);
		
		bizCmsArticleData.setTitle(bizCmsArticle.getTitle());
		returnResult.setData(bizCmsArticleData);
		
		return returnResult;
	}
}
